# This is a sample Python script.
import requests as req
from xml.dom.minidom import parseString
import datetime
#关闭https 证书警告
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

#功放网页版控制baseurl(依据实际情况而定)
def fun_getBaseUrl():
    urlBase = "https://192.168.50.79:10443"
    print("Https baseUrl=" + urlBase)
    return urlBase
def getDateTimeStr():
    now = datetime.datetime.now()
    timestamp = datetime.datetime.timestamp(now)
    return str(timestamp)

#获取天龙功放电源状态
def getDenonPowerState():
    payload = {'type': '4', '_': getDateTimeStr()}
    url = fun_getBaseUrl() + '/ajax/globals/get_config'
    r = req.get(url, params=payload,verify=False)
    print(r.status_code)
    print("获取功放开关机状态的接口地址=" + r.url)
    if (r.status_code == 200):
        print('获取功放开关机状态成功，返回数据如下：')
        print(r.text)
    else:
        print('获取功放开关机状态失败')
    return r

#执行开机动作
def power_on_denon_action():
    payload = {'type': '4','data':'<MainZone><Power>1</Power></MainZone>', '_': getDateTimeStr()}
    url = fun_getBaseUrl() + '/ajax/globals/set_config'
    r = req.get(url, params=payload,verify=False)
    print(r.status_code)
    print(r.text)
    print("功放开机接口地址=" + r.url)
    if(r.status_code==200):
        print("执行开机动作成功")
    else:
        print("执行开机动作失败")

#执行关机动作
def power_off_denon_action():
    payload = {'type': '4', 'data': '<MainZone><Power>3</Power></MainZone>', '_': getDateTimeStr()}
    url =fun_getBaseUrl() + '/ajax/globals/set_config'
    r = req.get( url, params=payload,verify=False)
    print(r.status_code)
    print(r.text)
    print("功放关机接口地址=" + r.url)
    if(r.status_code==200):
        print("执行关机动作成功")
    else:
        print("执行关机动作失败")

#天龙功放执行关机操作
def denon_exec_power_off():
    r = getDenonPowerState()
    print(r.status_code)
    if(r.status_code!=200):
        return
    doc = parseString(r.text)
    collection = doc.documentElement
    #获取MainZone电源当前当前状态
    MainZonePowerStateVaL = collection.getElementsByTagName("Power")[0].childNodes[0].data
    print(MainZonePowerStateVaL)
    if(MainZonePowerStateVaL is None):
        print('解析获取电源状态xml异常')
        return
    MainZonePowerStateVaLInt = int(MainZonePowerStateVaL)
    if(MainZonePowerStateVaLInt==1):
        #执行开机
        print('执行关机动作')
        power_off_denon_action()
    else:
        print('当前已经关机状态，无需执行关机动作')

# 天龙功放执行开机操作
def denon_exec_power_on():
    r = getDenonPowerState()
    print(r.status_code)
    if (r.status_code != 200):
        return
    doc = parseString(r.text)
    collection = doc.documentElement
    # 获取MainZone电源当前当前状态
    MainZonePowerStateVaL = collection.getElementsByTagName("Power")[0].childNodes[0].data
    print(MainZonePowerStateVaL)
    if (MainZonePowerStateVaL is None):
        print('解析获取电源状态xml异常')
        return
    MainZonePowerStateVaLInt = int(MainZonePowerStateVaL)
    if (MainZonePowerStateVaLInt != 1):
        # 执行开机
        print('执行开机动作')
        power_on_denon_action()
    else:
        print('当前已经开机，无需执行开机动作')

    # Press the green button in the gutter to run the script.
if __name__ == '__main__':
    denon_exec_power_off()

